package com.ztstkj.lzscale.adpters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.ztstkj.lzscale.R
import com.ztstkj.lzscale.adpters.CategroyAdapter.ICategoryItemClickListener
import com.ztstkj.lzservices.dto.RecyleItem
import com.ztstkj.lzservices.model.SettlementEnum

class RecyleAdapter(private var datas: List<RecyleItem>,private  var settlement: Int= SettlementEnum.POINT.value) : RecyclerView.Adapter<RecyleAdapter.ItemViewHolder>() {

    private var selectedPoisiton:Int= RecyclerView.NO_POSITION
    lateinit var context: Context
    var onClickListener: IRecyleItemClickListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        context = parent.context
        val v = LayoutInflater.from(parent.context).inflate(R.layout.activity_recyle_item, parent, false)
        return ItemViewHolder(v)
    }

    @SuppressLint("DiscouragedApi", "UseCompatLoadingForDrawables", "NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: ItemViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val bean = datas[position]
        holder.imgRecyleIcon.setImageDrawable(context.getDrawable(context.resources.getIdentifier(bean.iconName,"drawable",context.packageName)) )
        holder.txtRecyleName.text=bean.name
        if(settlement== SettlementEnum.CASH.value){
            holder.txtRecylePrice.text=bean.price.toString()+"元"
            bean.curPrice=bean.price.toString()+"元/kg"
        } else if(settlement== SettlementEnum.POINT.value){
            holder.txtRecylePrice.text=bean.point.toString()+"积分"
            bean.curPrice=bean.point.toString()+"积分/kg"
        }
        holder.onSelect(position==selectedPoisiton)
        holder.clRecyle.setOnClickListener {
            selectedPoisiton=position
            notifyDataSetChanged()
            onClickListener?.onClick(bean)
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val clRecyle: ConstraintLayout = view.findViewById(R.id.clRecyle)
        val imgRecyleIcon: ImageView = view.findViewById(R.id.imgRecyleIcon)
        val txtRecyleName: TextView = view.findViewById(R.id.txtRecyleName)
        val txtRecylePrice: TextView = view.findViewById(R.id.txtRecylePrice)

        @SuppressLint("UseCompatLoadingForDrawables")
        fun onSelect(isSelected: Boolean) {
            if (isSelected) {
                clRecyle.background = context.getDrawable(R.drawable.recyle_item_bg_selected)
            } else {
                clRecyle.background = context.getDrawable(R.drawable.recyle_item_bg)
            }
        }
    }
    //自定义接口
    interface IRecyleItemClickListener {
        fun onClick(item: RecyleItem)
    }
}